package com.bdqn.mzc.controller;

import com.bdqn.mzc.model.Region;
import com.bdqn.mzc.model.Result;
import com.bdqn.mzc.model.Scenicspot;
import com.bdqn.mzc.service.ShowInfoService;
import com.bdqn.mzc.utils.DataGridViewResult;
import com.bdqn.mzc.vo.NutriVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/home")
@CrossOrigin
@Slf4j
public class HomeShowController {
    @Autowired
    private ShowInfoService showInfoService;

    @ApiOperation("首页省份信息展示功能  " +
            " 热点最高的城市排名更靠前" +
            " 省份下景区每卖出一张票热度+1")
    @GetMapping("/province")
    @CrossOrigin
    public Result<List<Region>> homeProvinceShow(){
        return Result.wrapSuccessfulResult(showInfoService.homeProvinceShow());
    }

    @ApiOperation("首页省份信息展示功能  " +
            " 热点最高的城市排名更靠前" +
            " 省份下景区每卖出一张票热度+1")
    @GetMapping("/getPage")
    @CrossOrigin
    @ResponseBody
    public DataGridViewResult homeProvinceShow(NutriVo nutriVo){
        //设置分页信息  当前页码：nutriVo.getPage()  每页显示多少条数据nutriVo.getLimit()
        PageHelper.startPage(nutriVo.getPageIndex(),nutriVo.getPageSize());
        //调用分页查询账单列表的方法
        List<Region> finList =showInfoService.homeProvinceShow(nutriVo);
        //创建分页对象
        PageInfo<Region> pageInfo =new PageInfo<>(finList);

        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
//        return Result.wrapSuccessfulResult(showInfoService.homeProvinceShow(nutriVo));
    }

    @ApiOperation("根据Id获取景点  ")
    @GetMapping("/getInfoByRegionId")
    public Result<Region> getInfoByRegionId(Region region) {
        return Result.wrapSuccessfulResult(showInfoService.selectByPrimaryKey(region.getRegionId()));
    }

    @ApiOperation("根据Id更新景点  ")
    @PostMapping("/updateByPrimaryKeySelective")
    public Result<Integer> updateByPrimaryKeySelective(Region region,@RequestParam(value = "file",required = false) MultipartFile file) {
        Upload(region,file);
        return Result.wrapSuccessfulResult(showInfoService.updateByPrimaryKeySelective(region));
    }

    @ApiOperation("添加景点信息")
    @PostMapping("/insertSelective")
    public Result<Integer> insertSelective(Region region,@RequestParam(value = "file",required = false) MultipartFile file) {
            log.info(region.toString());
            Upload(region,file);
        return Result.wrapSuccessfulResult(showInfoService.insertSelective(region));
    }

    @ApiOperation("根据Id删除景点")
    @GetMapping("/deleteByPrimaryKey")
    public Result<Integer> deleteByPrimaryKey(Integer regionId) {
        return Result.wrapSuccessfulResult(showInfoService.deleteByPrimaryKey(regionId));
    }

    @ApiOperation("首页景区总数统计展示功能  ")
    @GetMapping("/sum")
    public Result<Integer> homeSumScenicsPot() {
        return Result.wrapSuccessfulResult(showInfoService.homeSumScenicsPot());
    }


    @ApiOperation("按照景区订单量排序显示功能  " +
            " 景区下单越多排名越靠前")
    @GetMapping("/order")
    public Result<List<Scenicspot>> homeOrderQuantityShow() {
        return Result.wrapSuccessfulResult(showInfoService.homeOrderQuantityShow());
    }

    @ApiOperation("按照景区评分排序显示  " +
            "  评分越高排名越靠前")
    @GetMapping("/grade")
    public Result<List<Scenicspot>> homeGradeShow() {
        return Result.wrapSuccessfulResult(showInfoService.homeGradeShow());
    }

    // 文件上傳
    public void Upload(Region region,@RequestParam(value = "file",required = false) MultipartFile file){
        try {
            // 动态获取文件的真实路径，用于保存上传的文件
            String filepath ="D:\\Program Files (x86)\\Gofar - 副本\\images\\img/";
            System.out.println(filepath);

            // 获取文件的全名
            String filename =  file.getOriginalFilename();
            log.info("文件全名："+filename);

            String suffixName =  filename.substring(filename.lastIndexOf("."));
            System.out.println("后缀名："+suffixName);

            //文件名称+后缀
            filename=UUID.randomUUID()+suffixName;

            String saveFileUrl=filepath+filename;
            System.out.println(saveFileUrl);
            String str = "images/img/";
            File savefile = new File(filepath+filename);
            file.transferTo(savefile);
            region.setRegionImg(str+filename);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

//    @PostMapping(value = "/test05")
//    @ApiOperation(value = "文件上传",notes = "使用@ResquestHeader,放在参数上,常使用在token验证")
//    public Object test05(
//            @RequestParam(value = "file",required = false) MultipartFile file){
//        try {
//            // 动态获取文件的真实路径，用于保存上传的文件
//            String filepath ="D:/Program Files (x86)/Gofar - 副本/images/img/";
//            System.out.println(filepath);
//
//            // 获取文件的全名
//            String filename =  file.getOriginalFilename();
//            System.out.println("文件全名："+filename);
//
//            String suffixName =  filename.substring(filename.lastIndexOf("."));
//            System.out.println("后缀名："+suffixName);
//
//            //文件名称+后缀
//            filename=UUID.randomUUID()+suffixName;
//
//            String saveFileUrl=filepath+filename;
//            System.out.println(saveFileUrl);
//
//            File savefile = new File(filepath+filename);
//            file.transferTo(savefile);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "成功";
//    }

}
